define(["../common"], function() {
	
	/**
	 * 注册事件
	 */
	
	// 购物小票 start
	
	$("#isPrintReceipt").click(function() {
		testIsPrintReceipt($(this).prop("checked"));
	});
	
	$("#isPrintReceiptQrcode").click(function() {
		testIsPrintReceiptQrcode($(this).prop("checked"));
	});
	
	$("[name='receiptQrcodeType']").click(function() {
		testReceiptQrcodeType(true, $(this).val());
	});
	
	function testIsPrintReceipt(isChecked) {
		$("#isPrintReceiptQrcode, #receiptFootText").prop("disabled", !isChecked);
		if (isChecked) {
			testIsPrintReceiptQrcode($("#isPrintReceiptQrcode").is(":checked"));
		} else {
			testIsPrintReceiptQrcode(false);
		}
	}
	
	function testIsPrintReceiptQrcode(isChecked) {
		var type = $("[name='receiptQrcodeType']:checked").val();
		$("[name='receiptQrcodeType']").prop("disabled", !isChecked);
		testReceiptQrcodeType(isChecked, type);
	}
	
	function testReceiptQrcodeType(isChecked, type) {
		$("#receiptQrcodeContent").prop("disabled", !(isChecked && type === "2"));
	}
	
	
	// 促销 start
	
	$("#isEnablePromote").click(function() {
		$("[name='enablePromoteType']").prop("disabled", !$(this).prop("checked"));
	});
	
	
	// 客户屏 start
	
	$("#isShowShoppingInfo").click(function() {
		$("#isShowShoppingList, #isShowShoppingQrcode").prop("disabled", !$(this).prop("checked"));
	});
	
	$("#isShowAd").click(function() {
		$("#showAdLazyTime, #cutAdLazyTime, [name='adType']").prop("disabled", !$(this).prop("checked"));
	});
	
	// 广告图片相关start#######################################

	var finder;
	var maxNum = 5;// 允许图片数
	var picItemHtml = $("#picSpan").html();
	$("#picSpan").empty().show();
	
	require(["ckfinder"], function() {
		// 图片库
		finder = new CKFinder();
		finder.connectorInfo = "attaType=STORE";
		finder.resourceType = 'Images';
		finder.selectActionData = "container";
		finder.selectActionFunction = function(fileUrl, data) {
			var itemHtml = picItemHtml
			.replace(/\-\{num\}\-/g, parseInt(Math.random() * 1e10))
			.replace(/\-\{pic\}\-/g, fileUrl)// 原图
			.replace(/\-\{url\}\-/g, fileUrl.replace("/images/", "/_thumbs/Images/"));// 缩略图
			$("#picSpan").append(itemHtml);
			
			maxNum--;
			
			$(window).scroll();// 触发显示图片
			
		};
	});
	

	// 事件：添加图片
	$("#bt_addPic").click(function() {
		if (maxNum === 0) {
			$(this).showMsg("图片数量已达上限", 'error');
		} else {
			finder.popup();		
		}
	});
	
	// 图片排序
	$(document).on('click', "[id^='picImg_']", function() {
		$("#picSpan").prepend($(this).parent());// span
	});

	// 删除图片
	$(document).on("click", "[id^='picDel_']", function() {
		$(this).parent().remove();// span
		maxNum++;
		return false;
	});

	
	/**
	 * 初始化显示
	 */ 
	
	var setting = null;
	/*
	{
		"print": {
			"isPrintReceipt": true,
			"receiptFootText": "谢谢惠顾，欢迎网上订购：",
			"isPrintReceiptQrcode": true,
			"receiptQrcodeType": 0,
			"receiptQrcodeContent": ""
		},
		"promote": {
			"isEnablePromote": false,
			"enablePromoteType": 0
		},
		"clientScreen": {
			"isOpenClientScreen": false,
			"isShowShoppingInfo": true,
			"isShowShoppingList": true,
			"isShowShoppingQrcode": true,
			"isShowShoppingChange": true,
			"isShowAd": true,
			"showAdLazyTime": 10,
			"cutAdLazyTime": 10,
			"adType": 0,
			"ads": []				
		}
	}
	*/
	$.getJSON(g_shopDomain + "/store/get/json", function(data) {
		setting = $.parseJSON(data.result.localSetting);
		
		$("#isPrintReceipt").prop("checked", setting.print.isPrintReceipt);
		$("#receiptFootText").val(setting.print.receiptFootText);
		$("#isPrintReceiptQrcode").prop("checked", setting.print.isPrintReceiptQrcode);
		$("[name=receiptQrcodeType][value='"+ setting.print.receiptQrcodeType +"']").prop("checked", true);
		$("#receiptQrcodeContent").val(setting.print.receiptQrcodeContent);
		
		$("#isEnablePromote").prop("checked", setting.promote.isEnablePromote);
		$("[name=enablePromoteType][value='"+ setting.promote.enablePromoteType +"']").prop("checked", true);
		
		$("#isOpenClientScreen").prop("checked", setting.clientScreen.isOpenClientScreen);
		$("#isShowShoppingInfo").prop("checked", setting.clientScreen.isShowShoppingInfo);
		$("#isShowShoppingList").prop("checked", setting.clientScreen.isShowShoppingList);
		$("#isShowShoppingQrcode").prop("checked", setting.clientScreen.isShowShoppingQrcode);
		$("#isShowShoppingChange").prop("checked", setting.clientScreen.isShowShoppingChange);
		$("#isShowAd").prop("checked", setting.clientScreen.isShowAd);
		$("#showAdLazyTime").val(setting.clientScreen.showAdLazyTime);
		$("#cutAdLazyTime").val(setting.clientScreen.cutAdLazyTime);
		$("[name=adType]").each(function(i, el) {
			if ((setting.clientScreen.adType & (1 << parseInt($(el).val()))) > 0) {
				$(el).prop("checked", true);
			}
		});
		
		var ads = setting.clientScreen.ads;
		for (var i in ads) {
			var itemHtml = picItemHtml
			.replace(/\-\{num\}\-/g, parseInt(Math.random() * 1e10))
			.replace(/\-\{pic\}\-/g, ads[i])// 原图
			.replace(/\-\{url\}\-/g, ads[i].replace("/images/", "/_thumbs/Images/"));// 缩略图
			$("#picSpan").append(itemHtml);
			maxNum--;
		}
		$(window).scroll();// 触发显示图片
		
		// 使正确显示禁用状态
		$("input[type=checkbox]:enabled").each(function(i, el) {
			$(this).click();
			$(this).click();
		});
		
		// TODO 暂时禁用未完成功能的选项
		$("#isEnablePromote").prop("disabled", true);
	})
	
	
	/**
	 * 保存
	 */
	
	$("#saveBt").click(function() {
		
		var adType = 0 ;
		$("[name=adType]:checked").each(function(i, el) {
			adType |=1 << parseInt($(el).val());
		});
		
		var ads = [];
		$("[id^='pic_']").each(function() {
			ads.push($(this).val().replace(g_attaDomain, ""));
		});
		
		setting = {
			print: {
				isPrintReceipt: $("#isPrintReceipt").prop("checked"),
				receiptFootText: $("#receiptFootText").val(),
				isPrintReceiptQrcode: $("#isPrintReceiptQrcode").prop("checked"),
				receiptQrcodeType: $("[name=receiptQrcodeType]:checked").val(),
				receiptQrcodeContent: $("#receiptQrcodeContent").val()
			},
			promote: {
				isEnablePromote: $("#isEnablePromote").prop("checked"),
				enablePromoteType: $("[name=enablePromoteType]:checked").val()
			},
			clientScreen: {
				isOpenClientScreen: $("#isOpenClientScreen").prop("checked"),
				isShowShoppingInfo: $("#isShowShoppingInfo").prop("checked"),
				isShowShoppingList: $("#isShowShoppingList").prop("checked"),
				isShowShoppingQrcode: $("#isShowShoppingQrcode").prop("checked"),
				isShowShoppingChange: $("#isShowShoppingChange").prop("checked"),
				isShowAd: $("#isShowAd").prop("checked"),
				showAdLazyTime: $("#showAdLazyTime").val(),
				cutAdLazyTime: $("#cutAdLazyTime").val(),
				adType: adType,
				ads: ads
			}
		}
		
		$.post(g_shopDomain + "/store/localSettingDo/json", 
			{localSetting: JSON.stringify(setting)}, 
			function(data) {
				if (data.status !== 0) {
					$(document).showMsg(data.message, 'error');
				}
				$(document).showMsg("保存成功！", 'pass');
			}
		);
	});
});
